package com.sms.project;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sms.DB.ConnectDB;
import com.sms.DB.OperateDB;
import com.sms.user.Student;

public class Project {
	private int proID;
	private int grade;//项目级别
	private String proName;
	private String proType;
	private String content;
	private String fromID;
	private String toID;
	private int groupID;
	private String startTime;
	private String endTime;
	private String status;
	private String isAppend;
	private String leaderID;//项目组长
	private String leaderName;
	
	private List<Map> appendFile = null;//项目附件
	private List<String> member = null;//项目成员名字
	private List<String> memberID = null;//项目成员ID
	
	//初始化一个项目
	public void init(Map map) {
			setProID((Integer)map.get("taskid"));
			setGrade((Integer)map.get("level"));
			setProName((String)map.get("taskname"));
			setProType((String)map.get("task_type"));
			setContent((String)map.get("content"));
			setFromID((String)map.get("fromid"));
			setToID((String)map.get("toid"));
			setGroupID(this.groupID);
			setStartTime(map.get("starttime").toString());
			setEndTime(map.get("endtime").toString());
			setStatus((String)map.get("status"));
			setLeaderID((Integer)map.get("taskid"));
			setLeaderName(getLeaderID());
			setIsAppend((String)map.get("isappend"));
			if(isAppend.equals("1")) {
				this.setAppendFile((Integer)map.get("taskid"));
			}
			
			setMember(getLeaderID(), getProID());
			setMemberID(this.memberID);
	}
	
	public int getGrade() {
		return grade;
	}

	public void setGrade(int grade) {
		this.grade = grade;
	}

	
	
	public int getGroupID() {
		return groupID;
	}

	public void setGroupID(int groupID) {
		this.groupID = groupID;
	}

	public String getLeaderID() {
		return leaderID;
	}

	public void setLeaderID(int proID) {
		String sql = "select * from groupinfo where projectid = " + proID;
		List<Map> list = new ArrayList<Map>();
		String leaderid = "";
		
		list = OperateDB.executeQuery(sql);
		if(list.size() > 0) {
			Map map = list.get(0);
			leaderid = (String)map.get("leaderid");
			this.groupID = (Integer)map.get("groupid");
		}
		this.leaderID = leaderid;
		
	}
	
	public String getLeaderName() {
		return leaderName;
	}

	public void setLeaderName(String leaderID) {
		String sql = "select sname from stuinfo where sno = '" + leaderID + "'";
		List<Map> list = new ArrayList<Map>();
		String leadername = "";
		
		list = OperateDB.executeQuery(sql);
		if(list.size() > 0) {
			Map map = list.get(0);
			leadername = (String)map.get("sname");
		}
		this.leaderName = leadername;
	}

	
	public List<Map> getAppendFile() {
		return appendFile;
	}
	
	//获取指定文件的附件并设置
	public void setAppendFile(int fileID) {
		String sql = "select * from appendinfo where append_type = '项目任务' " +
				"and fileid = " + fileID;
		List<Map> list = new ArrayList<Map>();
		list = OperateDB.executeQuery(sql);
		if(list.size() > 0) {//有附件时才初始化appendPath对象
			appendFile = new ArrayList<Map>();
			for(int i=0; i<list.size(); i++) {
				Map map = list.get(i);
				String fileName = (String)map.get("filename");
				String saveName = (String)map.get("savename");
				Map<String, String> map2 = new HashMap<String, String>();
				map2.put("fileName", fileName);
				map2.put("saveName", saveName);
				appendFile.add(map2);
			}
		}
	}
	
	public List<String> getMember() {
		return member;
	}
	
	//获得该项目的成员并设置
	public void setMember(String leaderID, int proID) {
		String sql = "select sname, memberid from groupinfo as gi, groupmember as gm, stuinfo as si " +
				" where gi.groupid = gm.groupid " +
				" and group_type = '项目组' " +
				" and projectid = " + proID + 
				" and leaderid = '" + leaderID + "' " +
				" and gm.memberid = si.sno";
		//System.out.println(sql);
		List<Map> list = new ArrayList<Map>();

		list = OperateDB.executeQuery(sql);
		if(list.size() > 0) {//项目有组员时才初始化member对象
			member = new ArrayList<String>();
			memberID = new ArrayList<String>();
			for(int i=0; i<list.size(); i++) {
				Map map = list.get(i);
				String memberName = (String)map.get("sname");
				String memberID = (String)map.get("memberid");
				member.add(memberName);
				this.memberID.add(memberID);
			}
		}
		
	}
	

	public List<String> getMemberID() {
		return memberID;
	}

	public void setMemberID(List<String> memberID) {
		this.memberID = memberID;
	}

	
	public int getProID() {
		return proID;
	}

	public void setProID(int proID) {
		this.proID = proID;
	}

	public String getProName() {
		return proName;
	}

	public void setProName(String proName) {
		this.proName = proName;
	}

	public String getProType() {
		return proType;
	}

	public void setProType(String proType) {
		this.proType = proType;
	}

	public String getContent() {
		return content;
	}
	
	public void setContent(String content) {
		this.content = content;
	}
	
	public String getFromID() {
		return fromID;
	}
	
	public void setFromID(String fromID) {
		this.fromID = fromID;
	}
	
	public String getToID() {
		return toID;
	}
	
	public void setToID(String toID) {
		this.toID = toID;
	}
	
	public String getStartTime() {
		return startTime;
	}
	
	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}
	
	public String getEndTime() {
		return endTime;
	}
	
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	
	public String getStatus() {
		return status;
	}
	
	public void setStatus(String status) {
		this.status = status;
	}
	
	public String getIsAppend() {
		return isAppend;
	}
	
	public void setIsAppend(String isAppend) {
		this.isAppend = isAppend;
	}
	
	
}



